package com.qdsg.modulesihealsdk.ecg12;

import android.util.Xml;

import org.xmlpull.v1.XmlPullParser;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public class AECGParse {
    /**
     * 解析AECG文件
     *
     * @param file
     * @return
     */
    public static short[] parseAECGRecord(File file) {
        short[] data = null;
        InputStream stream = null;
        XmlPullParser xmlParse = Xml.newPullParser();
        try {
            // get file stream and set encoding
            stream = new FileInputStream(file);
            xmlParse.setInput(stream, "utf-8");
            // get event type
            int evnType = xmlParse.getEventType();
            // continue to end document
            int dataOffset = 0;
            while (evnType != XmlPullParser.END_DOCUMENT) {
                switch (evnType) {
                    case XmlPullParser.START_TAG:
                        String tag = xmlParse.getName();
                        if (tag.equalsIgnoreCase("code")) {
                            evnType = xmlParse.next();
                            String value = xmlParse.getAttributeValue(0);
                            if (value.equalsIgnoreCase("MDC_ECG_LEAD_I")) {
                                dataOffset = 0;
                            } else if (value.equalsIgnoreCase("MDC_ECG_LEAD_II")) {
                                dataOffset = 1;
                            } else if (value.equalsIgnoreCase("MDC_ECG_LEAD_III")) {
                                dataOffset = 2;
                            } else if (value.equalsIgnoreCase("MDC_ECG_LEAD_AVR")) {
                                dataOffset = 3;
                            } else if (value.equalsIgnoreCase("MDC_ECG_LEAD_AVL")) {
                                dataOffset = 4;
                            } else if (value.equalsIgnoreCase("MDC_ECG_LEAD_AVF")) {
                                dataOffset = 5;
                            } else if (value.equalsIgnoreCase("MDC_ECG_LEAD_V1")) {
                                dataOffset = 6;
                            } else if (value.equalsIgnoreCase("MDC_ECG_LEAD_V2")) {
                                dataOffset = 7;
                            } else if (value.equalsIgnoreCase("MDC_ECG_LEAD_V3")) {
                                dataOffset = 8;
                            } else if (value.equalsIgnoreCase("MDC_ECG_LEAD_V4")) {
                                dataOffset = 9;
                            } else if (value.equalsIgnoreCase("MDC_ECG_LEAD_V5")) {
                                dataOffset = 10;
                            } else if (value.equalsIgnoreCase("MDC_ECG_LEAD_V6")) {
                                dataOffset = 11;
                            }
                        } else if (tag.equalsIgnoreCase("digits")) {
                            evnType = xmlParse.next();
                            String tmp = xmlParse.getText();
                            if (tmp != null) {
                                String[] array = tmp.split(" ");
                                if (array.length > 0) {
                                    if (dataOffset == 0) {
                                        data = new short[array.length * 12];
                                    }
                                    for (int i = 0; i < array.length; i++) {
                                        data[12 * i + dataOffset] = Short
                                                .parseShort(array[i]);
                                    }
                                }
                            }
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        break;
                    default:
                        break;
                }
                evnType = xmlParse.next();
            }
        } catch (Exception e) {
        }
        return data;
    }
}
